<?php

namespace api\services;

use api\models\AreaDict;
use api\utils\LogUtil;
use Exception;
use Yii;

class AreaService
{
    const META_LEVEL_1 = 0;
    const META_LEVEL_2 = 1;
    const META_LEVEL_3 = 2;
    const META_LEVEL_4 = 3;
    const META_LEVEL_5 = 4;
    const META_LEVEL_6 = 5;

    /**
     * 从数据中台同步 area 信息
     */
    public static function sync($level = self::META_LEVEL_1)
    {
        if ($level == 0) {
            self::getNextCode($level);
            return [];
        }
        $areas = AreaDict::findAll(['level' => $level - 1]);
        foreach ($areas as $area) {
            self::getNextCode($level, $area->code);
        }
    }

    private static function getNextCode($level, $parent = '')
    {
        if (isset($level)) {
            $params = ['type' => $level];
        }
        if (!empty($parent)) {
            $params['code'] = $parent;
        }
        if ($level > 0 && empty($params['code'])) {
            return;
        }
        $areas = Yii::$app->dataCenter->getAreas($params);
        if (empty($areas)) {
            return;
        }
        foreach ($areas as $area) {
            $area['level'] = $area['type'];
            unset($area['type']);
            if ($area['level'] == self::META_LEVEL_1 || empty($area['parent_code'])) {
                $area['full_codes'] = $area['code'];
            } else {
                $parent = AreaDict::findOne(['code' => $area['parent_code']]);
                if (empty($parent)) {
                    throw new Exception('上级单位不存在' . $area['parent_code']);
                }
                $area['full_codes'] = $parent->full_codes . ',' . $area['code'];
            }
            $model = AreaDict::findOne(['code' => $area['code']]);
            if (empty($model)) {
                $model = new AreaDict($area);
            } else {
                $model->setAttributes($area);
            }
            $model->save();
            $nextLevel = $level + 1;
            if ($nextLevel <= self::META_LEVEL_6) {
                self::getNextCode($nextLevel, $model->code);
            }
        }
    }

    /**
     * @return 五级 code
     */
    public static function getMetaLevelsByCode($code)
    {
        $area = AreaDict::findOne(['code' => $code]);
        if (empty($area)) {
            LogUtil::warn('Event area not found', 'flow-center');
            throw new Exception('Event area not found');
        }
        $levelCodes = explode(',', $area->full_codes);

        return [
            'meta_level1' => empty($levelCodes[0]) ? '0' : $levelCodes[0],
            'meta_level2' => empty($levelCodes[1]) ? '0' : $levelCodes[1],
            'meta_level3' => empty($levelCodes[2]) ? '0' : $levelCodes[2],
            'meta_level4' => empty($levelCodes[3]) ? '0' : $levelCodes[3],
            'meta_level5' => empty($levelCodes[4]) ? '0' : $levelCodes[4],
            'meta_level6' => empty($levelCodes[5]) ? '0' : $levelCodes[5],
        ];
    }

    /**
     * @return [
     *      'areaCode' => '04'
     *      'level' => 2,
     * ]
     */
    public static function getAreaCodeByMetaLevels($info)
    {
        $areaCode = $info['meta_level1'];
        $level = 0;
        if (!empty($info['meta_level2'])) {
            $areaCode = $info['meta_level2'];
            $level = 1;
        }
        if (!empty($info['meta_level3'])) {
            $areaCode = $info['meta_level3'];
            $level = 2;
        }
        if (!empty($info['meta_level4'])) {
            $areaCode = $info['meta_level4'];
            $level = 3;
        }
        if (!empty($info['meta_level5'])) {
            $areaCode = $info['meta_level5'];
            $level = 4;
        }
        if (!empty($info['meta_level6'])) {
            $areaCode = $info['meta_level6'];
            $level = 5;
        }
        return [
            'areaCode' => $areaCode,
            'level' => $level,
        ];
    }
}
